import msg from "../../../utils/msg";
import { getCourseInfoAdmin, getDutyCourseInfo } from "../../../utils/request/api";
import User from "../../../utils/user"
import { sr_data } from "./util";

// pages/user/s_worker/s_worker.ts

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[]as sr_data[],//筛选后的数据
    showData:[] as sr_data[],//真正渲染的数据(防止一下太多数据渲染不过来)
    course_list:[] as sr_data[],
    sign_type:[] as string[],
    user:{} as User,
    input:'' as string,
    //下拉菜单分类
    option2: [
      { text: '主修课程', value: 0 },
      { text: '通识选修课', value: 1 },
      { text: '全部', value: 2 },
    ],
    value2: 0,
  },
  change2(value:any){
    this.setData({
      value2:value.detail,
    });
    switch(value.detail){
      case 0:
        this.search2('主修课程');
        break;
      case 1:
        this.search2('通识选修课');
        break;
      case 2:
        this.search2('');
        break;
    }
  },
  search2(input:string){
    msg.Loading('加载中');
    let data = this.data.course_list;
    let showData=[] as sr_data[];
    if(input=="") {
      showData=this.data.course_list;
    }
    else 
    data.forEach((item,index)=>{
      if(item.c_type==input ){
        showData.push(item);
      }
    });
    this._setdata(showData);
    msg.hideLoading();
  },
  onClickLeft(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function() {
    msg.Loading('加载中');
    let user = new User;
    this.setData({
      user:user,
    });
    let resData={} as {sr_data:sr_data[],sign_type:string[]};
    //如果是管理员则显示所有课程
    if(user.isAdmin){
      resData=await getCourseInfoAdmin({openid:user.openid})
      .then((res:any)=>{
        return res.data.data;
      })
    }
    else{//课代表
      resData=await getDutyCourseInfo({s_code:user.code})
      .then((res:any)=>{
        return res.data.data;
      });
    }
    
    this.setData({
      course_list:resData.sr_data,
      sign_type:resData.sign_type,
    });
    this.search2('主修课程');
    this.search(this.data.input);
    msg.hideLoading();
  },
  //选择时间
  chooseTime(e:any){
    let course_id = e.currentTarget.dataset.course_id;
    wx.navigateTo({
      url:'/pages/user/s_worker/chooseTime/chooseTime?course_id='+course_id+'&sign_type='+JSON.stringify(this.data.sign_type),
    })
  },
  //获取输入
  getInput(e:any){
    let input = e.detail.value;
    this.setData({
      input:input,
    });
    this.search(input);
  },
  search(input:string){
    msg.Loading('加载中');
    let data = this.data.data;
    let showData=[] as sr_data[];
    if(input=="") {
      showData=this.data.data;
    }
    else 
    data.forEach((item,index)=>{
      if(item.c_name.includes(input)|| item.c_teacher==input|| item.c_type==input ){
        showData.push(item);
      }
    });
    this.setData({showData:showData});
    msg.hideLoading();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  //分层渲染
  //设置筛选结果
  _setdata(data:sr_data[]){
    let showData=[] as sr_data[];
    for(let i=0;(i<50&&i<data.length);i++)
      showData.push(data[i]);
    this.setData({
      data:data,
      showData:showData,
    });
  },
  //检测下拉渲染新数据
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    this._pushShowData();
  },
  //渲染新数据
  _pushShowData(){
    let data=this.data.data;
    let showData=this.data.showData;
    if(data.length<=showData.length)return;
    let start=showData.length+1;
    for(let i=start;(i<start+50&&i<data.length);i++)
      showData.push(data[i]);
    this.setData({
      showData:showData,
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})